package com.gopay.common.rcs.service.transadmit.impl;

import com.gopay.common.constants.proccode.ProcCodeEnum;
import com.gopay.common.constants.rcs.TransAdmittancePerm;
import com.gopay.common.constants.rcs.TransAdmittanceSwitch;
import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.domain.rcs.req.RcsTransAdmitRequest;
import com.gopay.common.domain.rcs.res.RcsTransAdmitResponse;
import com.gopay.common.domain.rcs.transadmit.RcsTransAdmittanceConfCust;
import com.gopay.common.rcs.manager.antiphish.RcsAntiPhishingCustManager;
import com.gopay.common.rcs.manager.antiphish.RcsAntiPhishingGlobalManager;
import com.gopay.common.rcs.manager.antiphish.RcsAntiPhishingViolateLogManager;
import com.gopay.common.rcs.manager.transadmit.RcsTransAdmitConfManager;
import com.gopay.common.rcs.service.transadmit.RcsTransAdmitCheckService;
import com.gopay.common.user.manager.CustCorpInfoQueryManager;
import com.gopay.common.util.FastJsonUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
/**
 * 交易准入Service
 * @author lizheng
 *
 */
@Service("rcsTransAdmitCheckService")
public class RcsTransAdmitCheckServiceImpl implements RcsTransAdmitCheckService {
    protected final static Log logger = LogFactory.getLog(RcsTransAdmitCheckServiceImpl.class);
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(RcsTransAdmitCheckServiceImpl.class);

    @Autowired
    protected CustCorpInfoQueryManager custCorpInfoQueryManager;
    @Autowired
    protected RcsAntiPhishingViolateLogManager rcsAntiPhishingViolateLogManager;
    @Autowired
    protected RcsAntiPhishingGlobalManager rcsAntiPhishingGlobalManager;
    @Autowired
    protected RcsAntiPhishingCustManager rcsAntiPhishingCustManager;
    
    @Resource(name = "rcsTransAdmitConfManager")
    protected RcsTransAdmitConfManager transAdmitCheckManager;
    @Override
    public RcsTransAdmitResponse ipAdmitCheck(RcsTransAdmitRequest req) {

        LOGGER.info("【校验备案IP】开始，请求数据：{}" , FastJsonUtil.toJSONString(req) );

        RcsTransAdmitResponse resp = new RcsTransAdmitResponse(ProcCodeEnum.PROC_CODE_200S1000.getCode());
        String ipOfRequest = req.getHeaderIP();
        if ("0:0:0:0:0:0:0:1".equals(ipOfRequest)){
            //本机访问，直接放过
            LOGGER.info("【校验备案IP】本地测试访问，跳过校验");
            return resp;
        }

        RcsTransAdmittanceConfCust conf = transAdmitCheckManager.findByCustIdAndMerTxnCd(req.getCustId(), req.getTranCode());

        LOGGER.info("【校验备案IP】merOrderId:"+req.getMerOrderId()+"。后台配置信息：{}" , FastJsonUtil.toJSONString(conf));
        
        if(null==conf){
        	logger.error("【校验备案IP】.merId:"+req.getCustId()+"merOrderId:"+req.getMerOrderId()+"未找到备案Ip配置");
        	resp.setProcCode(ProcCodeEnum.PROC_CODE_200E5004.getCode());
        	resp.setProcCodeMsg("未找到备案IP配置");
        	return resp;
        }
        if(StringUtils.equals(conf.getIpSwitch(), TransAdmittanceSwitch.CLAOSE.value)){
        	logger.info("【校验备案IP】merId:"+req.getCustId()+"，merOrderId:"+req.getMerOrderId()+"。IP校验关闭，跳过IP校验");
        	return resp;
        }
        boolean boo = false;
        logger.info("【校验备案IP】merId:"+req.getCustId()+"，merOrderId:"+req.getMerOrderId()+"，请求IP:"+ipOfRequest+"，备案Ip:" + conf.getAdmitIp());
        if(StringUtils.isNotBlank(conf.getAdmitIp())){
            String[] ipList;
            if(conf.getAdmitIp().indexOf(";")>-1){
                ipList = conf.getAdmitIp().split(";");
            }else{
                ipList = new String[]{conf.getAdmitIp()};
            }
            boolean match = false;
            for(int i=0;i<ipList.length; i++){
                if(ipOfRequest.equals(ipList[i])){
                    match = true;
                    break;
                }
            }
            boo = match;
        }
        //未配置  或者校验 不通过  都拒绝交易。
        if (!boo) {
            resp.setProcCode(ProcCodeEnum.PROC_CODE_200E5004.getCode());
            resp.setProcCodeMsg("IP校验不匹配");
        }
        LOGGER.info("【校验备案IP】结束，merId："+req.getCustId()+"，merOrderId："+req.getMerOrderId()+"。返回结果：{}" , FastJsonUtil.toJSONString(resp));
        return resp;
    }
    
    
    @Override
    public RcsTransAdmitResponse apiSinglePermCheck(RcsTransAdmitRequest req) {
        logger.info("网关api单笔权限校验.BEGIN." + req);
        CustCorpInfo cust = custCorpInfoQueryManager.get(req.getCustId());
        
        RcsTransAdmitResponse resp = new RcsTransAdmitResponse(ProcCodeEnum.PROC_CODE_200S1000.getCode());
        RcsTransAdmittanceConfCust conf = transAdmitCheckManager.findByCustIdAndIntTxnCd(cust.getCustId(), req.getIntTxnCd());
        logger.info("网关api单笔权限配置." + conf);
        if(null==conf||StringUtils.equals(conf.getApiSinglePerm(), TransAdmittancePerm.CLAOSE.value)){
        	resp.setProcCode(ProcCodeEnum.PROC_CODE_200W1001.getCode());
        }
        logger.info("网关api单笔权限校验.END." + resp);
        return resp;
    }
    @Override
    public RcsTransAdmitResponse webSinglePermCheck(RcsTransAdmitRequest req) {
    	logger.info("portal单笔权限校验.BEGIN." + req);
    	CustCorpInfo cust = custCorpInfoQueryManager.get(req.getCustId());
    	
    	RcsTransAdmitResponse resp = new RcsTransAdmitResponse(ProcCodeEnum.PROC_CODE_200S1000.getCode());
    	RcsTransAdmittanceConfCust conf = transAdmitCheckManager.findByCustIdAndIntTxnCd(cust.getCustId(), req.getIntTxnCd());
    	logger.info("portal单笔权限配置." + conf);
    	if(null==conf||StringUtils.equals(conf.getWebSinglePerm(), TransAdmittancePerm.CLAOSE.value)){
    		resp.setProcCode(ProcCodeEnum.PROC_CODE_200W1001.getCode());
    	}
    	logger.info("portal单笔权限校验.END." + resp);
    	return resp;
    }
    
    @Override
    public RcsTransAdmitResponse webBatchPermCheck(RcsTransAdmitRequest req) {
    	logger.warn("portal批量权限校验.BEGIN." + req);
    	CustCorpInfo cust = custCorpInfoQueryManager.get(req.getCustId());
    	
    	RcsTransAdmitResponse resp = new RcsTransAdmitResponse(ProcCodeEnum.PROC_CODE_200S1000.getCode());
    	RcsTransAdmittanceConfCust conf = transAdmitCheckManager.findByCustIdAndIntTxnCd(cust.getCustId(), req.getIntTxnCd());
    	logger.info("portal批量权限配置." + conf);
    	if(null==conf||StringUtils.equals(conf.getWebBatchPerm(), TransAdmittancePerm.CLAOSE.value)){
    		resp.setProcCode(ProcCodeEnum.PROC_CODE_200W1001.getCode());
    	}
    	logger.info("portal批量权限校验.END." + resp);
    	return resp;
    }
    

}
